package cn.cagurzhan.controller;

import cn.cagurzhan.domain.R;
import cn.cagurzhan.domain.entity.OperLog;
import cn.cagurzhan.service.AgriService;
import cn.cagurzhan.service.CacheService;
import cn.cagurzhan.service.LlmService;
import cn.cagurzhan.service.UploadService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import retrofit2.http.Multipart;

import java.util.List;
import java.util.Map;

/**
 * 农业控制器
 * @author AjaxZhan
 */
@RestController
@RequestMapping("/api/agri")
@RequiredArgsConstructor
@Slf4j
public class AgriController {

    private final AgriService agriService;

    private final LlmService llmService;

    private final UploadService uploadService;

    private final CacheService cacheService;

    /**
     * 获取所有日志消息
     */
    @GetMapping("/log/list")
    public R<List<OperLog>> listLog(){
        return agriService.listLog();
    }

    /**
     * 获取首页信息
     */
    @GetMapping("/info")
    public R<Map<String,Object>> getInfo(){
        return agriService.info();
    }

    /**
     * 调节灯光
     */
    @PostMapping("/light/{red}/{green}")
    public R<Void> light(@PathVariable("red")Integer red, @PathVariable("green") Integer green){
        return agriService.light(red,green);
    }

    /**
     * 灌溉
     */
    @PostMapping("/guangai/{which}/{power}")
    public R<Void> guangai(@PathVariable("which") Integer which, @PathVariable("power") Integer power){
        return agriService.guangai(which,power);
    }

    /**
     * 语言大模型决策
     */
    @PostMapping("/ask_ai")
    public R<Object> askAI() throws NoApiKeyException, InputRequiredException {
        return llmService.askAI(getInfo());
    }

    /**
     * 视觉大模型决策
     */
    @PostMapping(value= "/ask_vis",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Object> askVis(@RequestPart("file") MultipartFile img ) throws NoApiKeyException, UploadFileException {
        log.info("视觉大模型决策接口收到请求，img如    下:{}",img.getOriginalFilename());
        log.info("大小：{}",img.getSize());
        log.info("img对象：{}",img);
        // 上传到七牛云
        String address = uploadService.testUpload(img);
        // 利用视觉大模型决策
        return llmService.askVis(address);
    }

    @GetMapping("/get_ask_vis")
    public R<Object> getAskVis(){
        return R.ok(null,cacheService.get("vis"));
    }

    /**
     * 遮阳
     */
    @PostMapping("/shade/{power}")
    public R<Void> shade(@PathVariable("power") Integer power){
        return agriService.shade(power);
    }

    /**
     * 风扇
     */
    @PostMapping("/fans/{state}")
    public R<Void> fans(@PathVariable("state") Integer state){
        return agriService.fans(state);
    }

    /**
     * 语音控制，0表示关闭，1表示开启
     * @param status 状态
     */
    @PostMapping("/yuyin/{status}")
    public R<Void> openYuyin(@PathVariable("status") Integer status ){
        cacheService.set("yuyin",status.toString());
        return R.ok();
    }
}
